<template>
	<view class="content">
		<u-subsection :list="navLists" mode="button" fontSize="15px" @change="sectionChange" :current="currentIndex">
		</u-subsection>
		<u-loading-icon :show="showLoading"></u-loading-icon>
		<view class="main" v-show="!showLoading">
			<view class="lists" v-for="item,index in lists" :key="item.id">
				<view class="top">
					<view>编号：{{item.id}}</view>
					<u-count-down v-if="currentIndex==1" :time="item.time" format="HH:mm:ss">
					</u-count-down>
					<view class="status">{{statusLists[item.status+1]}}</view>
				</view>
				<view class="books" v-for="item1,index1 in item.books" :key="item1.saleBookId">
					<image v-if="ImgUrl" class="img" :src="ImgUrl+item1.bookImage" mode="aspectFit"></image>
					<view class="name">{{item1.bookName}}</view>
					<view class="price">￥{{item1.price}}</view>
				</view>
				<view class="bottom" v-if="currentIndex==1">
					<view class="total_price">Total：￥{{item.totalPrice}}</view>
					<view class="btn1" @click="deleteById" :data-id="item.id">cancel</view>
					<view class="btn2" @click="goToPay(item.id)">pay</view>
				</view>
				<view class="bottom" v-if="currentIndex==2">
					<view class="total_price">Paid：￥{{item.totalPrice}}</view>
				</view>
			</view>
		</view>
		<u-empty :show="isShow&&!showLoading" mode="order" icon="http://cdn.uviewui.com/uview/empty/order.png">
		</u-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navLists: ['全部', '待付款', '待发货', '待收货', '已取消'],
				statusLists: ['已取消', '待付款', '待发货'],
				currentIndex: 0,
				lists: [],
				orderLists: [],
				paidOrderLists: [],
				invalidOrderLists: [],
				waitReceiveLists: [],
				allLists: [],
				BaseUrl: '',
				ImgUrl: '',
				isShow: false,
				time: 0,
				showLoading: false
			}
		},
		onShow() {
			this.BaseUrl = getApp().globalData.BaseUrl
			this.ImgUrl = getApp().globalData.ImgUrl
			this.token = uni.getStorageSync('token')
			this.iniit()
			setTimeout(() => {
				this.showLoading = false
			}, 2000)
		},
		onShareTimeline() {
			let title = '念旧书-校园闲置书籍跳蚤市场'
			return {
				title: title,
				path: 'pages/index/index',
				imageUrl: '../../static/logo.png'
			}
		},
		onShareAppMessage: function(e) {
			let title = '念旧书-校园闲置书籍跳蚤市场'
			return {
				title: title,
				path: 'pages/index/index',
				imageUrl: '../../static/logo.png'
			}
		},
		methods: {
			async iniit() {
				const page = await getCurrentPages()
				let x = page.length - 1
				let options = page[x].options
				this.currentIndex = options.type
				// // console.log(options.type)
				if (!options.type) {
					options.type = 0
				}
				if (options.type == 0) {
					this.getAll()
				} else if (options.type == 1) {
					this.init()
				} else if (options.type == 2) {
					this.getPaidOrderLists()
				} else if (options.type == 4) {
					this.getIvalidOrderLists()
				}
			},
			async init() {
				try {
					await uni.request({
						url: this.BaseUrl + '/order/userOrder',
						header: {
							'Content-Type': 'application/json',
							'token': this.token
						},
						data: {
							status: 0
						},
						success: (res) => {
							// // console.log("init", res)
							if (res.data.data) {
								res.data.data.forEach(item => {
									let date = new Date(item.createTime)
									let now = new Date()
									item.time = 600000 - (now - date)
								})
							}
							this.orderLists = res.data.data
							this.lists = this.orderLists
							if (this.lists) {
								this.lists.reverse()
							}
							if(res.data.data){
								if (res.data.data.length === 0) {
									this.isShow = true
								}
							}
						}
					})
				} catch (res) {
					// // console.log(res)
				}
			},
			getAll() {
				this.showLoading = true
				this.init()
				this.getPaidOrderLists()
				this.getIvalidOrderLists()
				setTimeout(() => {
					this.allLists=[]
					if(this.orderLists){
						this.orderLists.forEach(item=>{
							this.allLists.push(item)
						})
					}
					if(this.paidOrderLists){
						this.paidOrderLists.forEach(item=>{
							this.allLists.push(item)
						})
					}
					if(this.invalidOrderLists){
						this.invalidOrderLists.forEach(item=>{
							this.allLists.push(item)
						})
					}
					// this.allLists = [...this.orderLists, ...this.paidOrderLists, ...this.invalidOrderLists]
					this.lists = this.allLists
					if (this.lists.length !== 0) {
						this.isShow = false
					}
					this.showLoading = false
				}, 500)
			},
			async getPaidOrderLists() {
				await uni.request({
					url: this.BaseUrl + '/order/userOrder',
					header: {
						'Content-Type': 'application/json',
						'token': this.token
					},
					data: {
						status: 1
					},
					success: (res) => {
						if (res.data.data) {
							res.data.data.forEach(item => {
								let date = new Date(item.createTime)
								let now = new Date()
								item.time = 600000 - (now - date)
							})
						}
						this.paidOrderLists = res.data.data
						this.lists = this.paidOrderLists
						if (this.lists) {
							this.lists.reverse()
						}
						if(this.paidOrderLists){
							if (this.paidOrderLists.length === 0) {
								this.isShow = true
							}
						}	
					}
				})
			},
			async getIvalidOrderLists() {
				await uni.request({
					url: this.BaseUrl + '/order/userOrder',
					header: {
						'Content-Type': 'application/json',
						'token': this.token
					},
					data: {
						status: -1
					},
					success: (res) => {
						// // console.log("invalid", res)
						if (res.data.data) {
							res.data.data.forEach(item => {
								let date = new Date(item.createTime)
								let now = new Date()
								item.time = 600000 - (now - date)
							})
						}
						this.invalidOrderLists = res.data.data
						this.lists = this.invalidOrderLists
						if (this.lists) {
							this.lists.reverse()
						}
						if (this.invalidOrderLists) {
							if (this.invalidOrderLists.length === 0) {
								this.isShow = true
							}
						}
					}
				})
			},
			async goToPay(id) {
				uni.navigateTo({
					url: "../pay/pay?orderId=" + id
				})
				// await uni.request({
				// 	url: this.BaseUrl + '/order/pay',
				// 	header: {
				// 		'Content-Type': 'application/json',
				// 		'token': this.token
				// 	},
				// 	data: {
				// 		orderId: id
				// 	},
				// 	success: (res) => {
				// 		this.orderLists.forEach((item, index) => {
				// 			if (item.id == id) {
				// 				this.orderLists.splice(index, 1)
				// 			}
				// 			if (this.orderLists.length === 0) {
				// 				this.isShow = true
				// 			}
				// 		})
				// 	}
				// })
			},
			sectionChange(index) {
				this.currentIndex = index;
				this.isShow = false
				if (index === 2) {
					this.getPaidOrderLists()
				} else if (index === 0) {
					this.getAll()
				} else if (index === 1) {
					this.init()
				} else if (index === 4) {
					this.getIvalidOrderLists()
				} else if (index === 3) {
					this.lists = this.waitReceiveLists
					this.isShow = true
				}
			},
			deleteById(e) {
				// // console.log(e)
				let orderId = e.currentTarget.dataset.id
				uni.showModal({
					content: "取消该订单？",
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: this.BaseUrl + '/order/cancel',
								header: {
									'Content-Type': 'application/json',
									'token': this.token
								},
								data: {
									orderId: orderId
								},
								success: (res) => {
									// // console.log(res)
									this.init()
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: 100%;
		background-color: rgb(244, 244, 246);

		.main {
			.lists {
				margin-top: 20upx;
				background-color: #fff;
				border-radius: 10upx;
				box-shadow: 0 0 5upx #888;

				.top {
					background-color: #fff;
					padding: 20upx 20upx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					color: #888;
					font-size: 25upx;
					border-bottom: 1upx solid #cecece;

					.status {
						color: #ff5500;
						font-weight: bold;
					}
				}

				.books {
					display: flex;
					padding: 20upx;
					border-bottom: 1upx solid #e3e3e3;
					position: relative;

					.img {
						width: 180upx;
						height: 180upx;
						box-shadow: 0 0 5upx #888;
					}

					.name {
						font-weight: bold;
						position: relative;
						left: 30upx;
					}

					.price {
						font-size: 28upx;
						font-weight: bolder;
						position: absolute;
						top: 150upx;
						left: 230upx;
					}
				}

				.bottom {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 20upx;
					position: relative;

					.total_price {
						font-size: 27upx;
					}

					.btn1 {
						height: 60upx;
						width: 170upx;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 30upx;
						box-shadow: 0 0 2upx #888;
						font-size: 25upx;
						background-color: rgb(239, 239, 236);
						position: absolute;
						right: 200upx;
					}

					.btn2 {
						height: 60upx;
						width: 170upx;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 30upx;
						box-shadow: 0 0 2upx #888;
						font-size: 25upx;
						background-color: rgb(232, 183, 103);
						color: #fff;
					}
				}
			}
		}
	}
</style>
